package com.eric.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.po.EquipmentPhoto;
import com.eric.mapper.EquipmentPhotoMapper;
import com.eric.service.IEquipmentPhotoService;
import com.eric.utils.ServerBASEURL;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 设备表中的图片存储 服务实现类
 * </p>
 *
 * @author 新哥
 * @since 2024-04-01
 */
@Service
@Transactional
public class EquipmentPhotoServiceImpl extends MPJBaseServiceImpl<EquipmentPhotoMapper, EquipmentPhoto> implements IEquipmentPhotoService {

//    数据库删除工作
    public void deleteByPhotoUrlList(List<String> photoUrlList){
        List<EquipmentPhoto>list=new ArrayList<>();
//        遍历photoUrlList
        for (String photoUrl : photoUrlList) {
//            去除了ServerBASEURL.BASEURL的部分，得到了新的photoUrl
            String newPhotoUrl = StrUtil.removePrefix(photoUrl, ServerBASEURL.BASEURL);
            EquipmentPhoto equipmentPhoto = lambdaQuery()
                    .eq(EquipmentPhoto::getPhotoUrl, newPhotoUrl)
                    .one();
            if (!BeanUtil.isEmpty(equipmentPhoto)){
                list.add(equipmentPhoto);
            }
        }
//        如果列表不为空，执行数据库移除，否则，什么都不做
        if (!list.isEmpty()){
            removeByIds(list);
        }
    }

    @Override
    public List<EquipmentPhoto> getEquipmentEncodeList(String encode) {
        return query().eq("equipment_encode", encode).list();
    }
}
